<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button v-if="isAuth('sys:school:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-button v-if="isAuth('sys:school:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
      <el-form-item>
        <div class="getExcel">
          <download-excel :data="excelData" :fields="json_fields" :header="excelTitle" :name="excelFName">
            <el-button type="primary" :disabled="excelBtnDis" @click="isEmtpy()" size="small">全部导出为excel</el-button>
          </download-excel>
          <el-form class="excelForm" :label-position="'left'">
            <el-form-item label="excel表标题:">
              <el-input size="small" placeholder="表格标题" style="width:160px" v-model="excelTitle"></el-input>
            </el-form-item>
            <el-form-item label="excel文件名:">
              <el-input size="small" placeholder="文件名" style="width:160px" v-model="excelFName">
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-form-item>
    </el-form>

    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50" fixed="left">
      </el-table-column>
      <el-table-column prop="id" header-align="center" align="center" label="项目id" width="75" fixed="left">
      </el-table-column>
      <!--      <el-table-column prop="userId" header-align="center" align="center" label="用户id">
      </el-table-column> -->
      <el-table-column prop="college" header-align="center" align="center" label="所属学院" fixed="left">
      </el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="项目标题" fixed="left" width="250">
      </el-table-column>
      <el-table-column prop="outline" header-align="center" align="center" label="项目概要">
      </el-table-column>
      <el-table-column prop="file" header-align="center" align="center" label="文件名">
      </el-table-column>
      <el-table-column prop="teacherName" header-align="center" align="center" label="指导老师">
      </el-table-column>
      <!--      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
      </el-table-column> -->
      <!--      <el-table-column prop="sum" header-align="center" align="center" label="校级评分总分">
      </el-table-column> -->
      <el-table-column prop="average" header-align="center" align="center" label="校评审平均分">
      </el-table-column>
      <el-table-column prop="peopleNumber" header-align="center" align="center" label="校评分总人数">
      </el-table-column>
      <el-table-column prop="level" header-align="center" align="center" label="项目等级" width="130">

        <template slot-scope="scope">
          <div>
            <div class="tag" v-if="scope.row.level">
              <el-tag effect="dark" v-if="scope.row.level == '校级'">校级</el-tag>
              <el-tag effect="dark" type="success" v-else-if="scope.row.level == '省级'">省级</el-tag>
              <el-tag effect="dark" type="warning" v-else-if="scope.row.level == '国家级'">国家级</el-tag>
              <el-tag :effect="'dark'" type="danger" v-else-if="scope.row.level == '不予立项'">不予立项</el-tag>
            </div>
            <el-select placeholder="请选择" v-model="scope.row.level"
              @change="AddOrUpdateLevel(scope.row.level, scope.row.id, scope.row)" v-else>
              <el-option v-for="k in objLevelList" :key="k" :label="k" :value="k">
              </el-option>
            </el-select>
          </div>
        </template>

      </el-table-column>
      <el-table-column prop="collegeAverage" header-align="center" align="center" label="院级评审平均分">
      </el-table-column>
      <el-table-column prop="projectType" header-align="center" align="center" label="比赛类型" width="80">
      </el-table-column>
      <el-table-column prop="type" header-align="center" align="center" label="项目类型">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button type="text" size="small" @click="repulse(scope.row.id, scope.row.title)">打回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './school-add-or-update'
export default {
  data() {
    return {
      objLevelList: ['校级', '省级', '国家级', '不予立项'],
      status: [{
        id: 0,
        statu: '审核中',
      }, {
        id: 1,
        statu: '通过',
      }, {
        id: 2,
        statu: '不通过',
      },],
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,

      json_fields: {
        '项目标题': 'title',
        '项目类型': 'type',
        '项目年限': 'projectType',
        '项目概要': 'outline',
        '负责人': 'leader',
        '指导老师': 'teacherName',
        '立项材料': 'file',
        '平均分': 'average',
        '评分人数': 'peopleNumber'
      },
      excelTitle: '',
      excelFName: '',
      excelData: [],
      ids: [],
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  computed: {
    excelBtnDis() {
      if ((this.excelTitle !== '') && (this.excelFName !== '') && (this.excelData.length !== 0)) return false
      return true
    },
  },
  methods: {
    //打回
    repulse(id, name) {
      let title = '你确定要将该项目打回吗'
      let content = '项目标题：' + name
      this.$confirm(content, title, {
        center: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/school/refer'),
          method: 'post',
          data: this.$http.adornData({
            id,
            statu: 3
          })
        }).then(({
          data
        }) => {
          if (data && data.code == 0) {
            this.$message.success('已打回项目')
            this.getDataList()
          } else {
            this.$message.warning(data.msg)
          }
        })
      }).catch((action) => {
        if (action == 'cancel')
          this.$message.warning('取消打回')
        this.getDataList()
      })



    },
    //评级
    AddOrUpdateLevel(level, id, i) {
      let title = '一旦提交无法修改'
      let content = '你确定要对项目：' + i.title + '评定等级为' + level + '吗'
      this.$confirm(content, title, {
        center: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/school/levelUpdate'),
          method: 'post',
          params: {
            'id': id,
            'level': level,
          }
        }).then(({
          data
        }) => {
          if (data.code == 0) {
            this.$message({
              type: 'success',
              message: '提交成功'
            })
            this.getDataList()
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            })
            this.getDataList()
          }
        })
      }).catch((action) => {
        if (action === 'cancel') {
          console.log('qx');
          this.getDataList()
        }
      })

    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/school/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        })
      }).then(({
        data
      }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
      this.excelData = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/school/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    isEmtpy() {
      if (this.excelData.length == 0) {
        this.$message.warning('请先选择要导出的项目')
      }
    },
    async getExcelData(ids) {
      // if (this.ids.length != 0) {
      //   let data = ids
      //   // let res = await post('/sys/college/downexcel', data)
      //   this.$http({
      //     url: this.$http.adornUrl('/sys/college/downexcel'),
      //     method: 'post',
      //     data,
      //   }).then(({
      //     data
      //   }) => {
      //     console.log('getList');
      //   })

      // } else if (this.ids.length == 0) {
      //   this.excelData = []
      // }
    },
  }
}
</script>


<style lang="scss">
.getExcel {
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
  // background-color: #000;
  // justify-content: space-between;
  // margin: 10px 0;
  background-color: #fff;
  border-radius: 15px;
  padding: 0px 20px;
  box-sizing: border-box;

  .excelForm {
    // display: flex;
    // padding: 10px 0;
    // height: 60px;
    // background-color: #f2f3f5;
    box-sizing: border-box;
    width: 600px;

  }
}
</style>
